package com.alldios.deposit.web;

import com.alibaba.fastjson.JSON;
import com.alldios.deposit.api.HfResp;
import com.alldios.deposit.api.HfTools;
import com.alldios.deposit.api.bid.AddBidInfoResp;
import com.alldios.deposit.api.bid.FullBidResp;
import com.alldios.deposit.api.invest.InitiativeTenderResp;
import com.alldios.deposit.api.pay.CashResp;
import com.alldios.deposit.api.pay.DirectRechargeResp;
import com.alldios.deposit.api.pay.QuickBindingResp;
import com.alldios.deposit.api.user.CorpGZBRegisterResp;
import com.alldios.deposit.api.user.CorpRegisterResp;
import com.alldios.deposit.api.user.GZBPinUpdateResp;
import com.alldios.deposit.api.user.UserRegisterResp;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;


@Controller
@RequestMapping("gateway")
public class GatewayController {

    private static Logger logger = LoggerFactory.getLogger(GatewayController.class);

    @RequestMapping("hfreturn")
    @ResponseBody
    public Object hfreturn(HttpServletRequest request) {
        String cmd = request.getParameter("CmdId");
        if (StringUtils.isNotEmpty(cmd)) {
            HfResp resp = null;
            switch (cmd) {
                case "UserRegister":
                    resp = HfTools.convert(request, UserRegisterResp.class);
                    break;
                case "CorpRegister":
                    resp = HfTools.convert(request, CorpRegisterResp.class);
                    break;
                case "GZBPinUpdate":
                    resp = HfTools.convert(request, GZBPinUpdateResp.class);
                    break;
                case "DirectRecharge":
                    resp = HfTools.convert(request, DirectRechargeResp.class);
                    break;
                case "Cash":
                    resp = HfTools.convert(request, CashResp.class);
                    break;
                case "AddBidInfo":
                    resp = HfTools.convert(request, AddBidInfoResp.class);
                    break;
                case "CorpGZBRegister":
                    resp = HfTools.convert(request, CorpGZBRegisterResp.class);
                    break;
                case "InitiativeTender":
                    resp = HfTools.convert(request, InitiativeTenderResp.class);
                    break;
                case "QuickBinding":
                    resp = HfTools.convert(request, QuickBindingResp.class);
                    break;
            }
            logger.info("hfreturn return --> {} , details --> {}", HfTools.verify(resp), JSON.toJSONString(resp));
        }
        return null;
    }

    @RequestMapping("hfnotice")
    @ResponseBody
    public Object hfnotice(HttpServletRequest request) {
        String cmd = request.getParameter("CmdId");
        cmd = StringUtils.isEmpty(cmd) ? request.getParameter("RespType") : cmd;
        if (StringUtils.isNotEmpty(cmd)) {
            HfResp resp = null;
            switch (cmd) {
                case "UserRegister":
                    resp = HfTools.convert(request, UserRegisterResp.class);
                    break;
                case "CorpRegister":
                    resp = HfTools.convert(request, CorpRegisterResp.class);
                    break;
                case "GZBPinUpdate":
                    resp = HfTools.convert(request, GZBPinUpdateResp.class);
                    break;
                case "DirectRecharge":
                    resp = HfTools.convert(request, DirectRechargeResp.class);
                    break;
                case "Cash":
                    resp = HfTools.convert(request, CashResp.class);
                    if (resp != null) {
                        logger.info("cash notice --> needEncrypt : {}", resp.needEncrypt());
                    }
                    break;
                case "AddBidInfo":
                    resp = HfTools.convert(request, AddBidInfoResp.class);
                    break;
                case "CorpGZBRegister":
                    resp = HfTools.convert(request, CorpGZBRegisterResp.class);
                    break;
                case "InitiativeTender":
                    resp = HfTools.convert(request, InitiativeTenderResp.class);
                    break;
                case "FullBid":
                    resp = HfTools.convert(request, FullBidResp.class);
                    break;
                case "QuickBinding":
                    resp = HfTools.convert(request, QuickBindingResp.class);
                    break;
            }
            logger.info("hfreturn hfnotice --> {} , details --> {}", HfTools.verify(resp), JSON.toJSONString(resp));
            return JSON.toJSONString(resp);
        }
        return null;
    }
}
